import {queryAll, deleteAll, query, modify, create, remove} from '../services/price';
import { queryAll as fetchCustList } from '../services/customerInformations';
import { message } from 'antd';
export default {
  namespace: 'price',
  state: {
    load : true,
    list: [],
    visible:{
    show:false,
    },
    record:{},
    selectedRows:[],
    selectedRowKeys:[],
    index:-1,
    customerList:[],
    currentItem: {},
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const pathname = location.pathname
        if(pathname === '/price'){
          dispatch({
            type: 'query',
            payload: location.query,
          })
          dispatch({
            type: 'fetchCustList',
          })
        }
      })
    },
  },

  effects: {
    *search({ payload }, {call, put,select }){
      const { customerNum,product, price, cost, ladderQuotation  }= payload;
      const list = yield call(queryAll)
      let data = list;
        //如果是数字类型，要用String(Num)方法转成字符串才能用match
      if(product){
        data=data.filter((item)=>item.product && item.product.match(product));
      }
      if(price){
        data=data.filter((item)=>item.price && item.price.match(price));
      }
      if(customerNum){
        data=data.filter((item)=>item.customerNum==customerNum);
      }

      if(data){
        yield put({
          type: 'querySuccess',
          payload: {
            list: data,
          },
        })
      }
    },

    *create({payload},{ call, put}){
        yield call(create,payload);
        yield put({type:'query'});
    },

    *modify({payload}, {call, put}){
      yield call(modify,payload),
      yield put({type:'query'})
    },

    *'delete'({payload},{call,put}){
        yield call(deleteAll,payload),
        yield put({type:'query'})
        message.warning('删除成功！',1)
    },

    *'delete2'({payload},{call,put}){
        yield call(deleteAll,[payload]),
        yield put({type:'query'})
        message.warning('删除成功！',1)
    },

    *fetchCustList({payload},{call,put}){
      const customerList = yield call(fetchCustList);
      yield put({
          type: 'save',
          payload:{customerList}
         });
    },

    *query({ payload }, { call, put }){
        const data = yield call(queryAll)
        yield put ({type: 'hideLoading'})

        if(data){
          yield put({
            type: 'querySuccess',
            payload: {
              list: data,
            },
          })
        }
      },


  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    querySuccess (state, action) {
      const { list } = action.payload
      return { ...state,
        list,
       }
    },

    showModal (state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal (state) {
      return { ...state, modalVisible: false }
    },

    //隐藏加载
    hideLoading (state,action) {
      return {
        ...state,load: false
      }
    },

  },
};
